﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace iRemote.Class
{
    class DataFormat
    {
        
        /*
         * 获取发送内容byte数组
         * 功能：组装发送数据
         */
        public static Byte[] getSendData(Byte real_operate_code,Double real_data) 
        {

            Byte struct_length = 0x05; //数据结构长度
            Byte start_code = 0xFB; //开始标志
            Byte operation_code = real_operate_code;//操作码
            Byte operation_data_high = 0x00; //操作数据高位
            Byte operation_data_lower = 0x00; //操作数据低位
            Byte stop_code = 0xFE; //结束标志


            int byte_length = struct_length + 1; //发送数据字节数组长度
            Byte[] all_send_data = new Byte[byte_length]; //发送数据字节数组初始化
            
            /*
             * 发送数据字节数组填充
             */
            all_send_data[0] = struct_length; 
            all_send_data[1] = start_code;
            all_send_data[2] = operation_code;
            if (real_data >=0 && real_data <=1)
            {
                short new_real_data = Convert.ToInt16(real_data * 4000);
                operation_data_high = Convert.ToByte(new_real_data / 256);
                operation_data_lower = Convert.ToByte(new_real_data % 256);
            }
            all_send_data[3] = operation_data_high;
            all_send_data[4] = operation_data_lower;
            all_send_data[byte_length-1] = stop_code;
            return all_send_data; //返回发送数据字节数组

        }

        /*
         * 根据标志返回对应数据的字节数组
         */
        public static Byte[] getNeedData(Byte[] data_bytes,String data_type) 
        {
            byte[] need_data = new byte[2];
            if (data_bytes.Length == iRemote.receive_data_length)
            {
                switch (data_type)
                {
                    case "Output voltage":
                        Array.Copy(data_bytes, 3, need_data, 0, 2);
                        break;
                    case "Output current":
                        Array.Copy(data_bytes, 5, need_data, 0, 2);
                        break;
                    case "Main Voltage":
                        Array.Copy(data_bytes, 7, need_data, 0, 2);
                        break;
                    case "Field Voltage":
                        Array.Copy(data_bytes, 9, need_data, 0, 2);
                        break;
                    case "Field current":
                        Array.Copy(data_bytes, 11, need_data, 0, 2);
                        break;
                    case "Hour":
                        Array.Copy(data_bytes, 13, need_data, 0, 2);
                        break;
                    case "Minute Seconds":
                        Array.Copy(data_bytes, 15, need_data, 0, 2);
                        break;
                    case "Gain":
                        Array.Copy(data_bytes, 17, need_data, 0, 2);
                        break;
                    case "Error 1":
                        Array.Copy(data_bytes, 19, need_data, 0, 2);
                        break;
                    case "Error 2":
                        Array.Copy(data_bytes, 21, need_data, 0, 2);
                        break;
                    case "Error 3":
                        Array.Copy(data_bytes, 23, need_data, 0, 2);
                        break;
                    default:
                        need_data[0] = 0x00;
                        need_data[1] = 0x00;
                        break;
                }
            }
            else 
            {
                need_data[0] = 0x00;
                need_data[1] = 0x00;
            }
            return need_data;
            
        }

        /*
         * 根据偏移量获取对应数据（字节）
         */
        public static Byte getNeedByte(Byte[] data_bytes, int index)
        {
            Byte need_byte = new Byte();
            if (data_bytes.Length == iRemote.receive_data_length && index >= 0 && index < iRemote.receive_data_length)
            {
                need_byte = data_bytes[index];
            }
            else
            {
                need_byte = 0;
            }

            return need_byte;
        }

        /*
         * 根据偏移量获取对应字节的对应位数的值
         */
        public static int getNeedByteBit(Byte[] data_bytes, int index,int bit_index){
            Byte need_byte = getNeedByte(data_bytes,index);
            int bit_value = getBitValue(need_byte, bit_index);
            return bit_value;
        }
        /*
         * 计算数据具体的值（字节数组类型到double类型）
         */
        public static double data_Calculation(Byte[] data_bytes) 
        {
            double data_show;
            if (data_bytes.Length == 2)
            {
                byte high = data_bytes[0];
                byte lower = data_bytes[1];

                data_show = data_bytes[1] + (data_bytes[0] % 32 * 256);
                if (DataFormat.getBitValue(high,8) == 1)
                {
                    data_show = data_show / 10;
                }
            }
            else 
            {
                data_show =  - 1;
            }
            data_show = Convert.ToDouble(Convert.ToInt32(data_show * 10)) / 10;
            return data_show;
        }

        /*
         * 获取字节中某一位的值
         */
        public  static int getBitValue(byte input, int index)
        {
            if (index > sizeof(byte)*8)
            {
                return -1;
            }
            //左移到最高位
            int value = (byte)(input << (sizeof(byte) * 8  - index));
            //右移到最低位
            value = value >> (sizeof(byte) * 8 - 1);
            return value;
        }

        /*
        * 设置字节中某一位的值
        */
        private static byte setBitValue(byte data, int index, bool flag)
        {
            if (index > 8 || index < 1)
                throw new ArgumentOutOfRangeException();
            int v = index < 2 ? index : (2 << (index - 2));
            return flag ? (byte)(data | v) : (byte)(data & ~v);
        }


    }
}
